Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

174
Visualizações
Pegjs Expected [0-9.] or end of input but "a" found

I want to handle string starts with number using pegjs. When I input with 1abcd it throws Expected [0-9.] or end of input but "a" found.. The expected output is { item: '1abcd', case: '=' }. What changes do I need?


var parser = peg.generate(`
  start
    = number:NUMBER  { return { case: "=", item: number } } 
    / string:STRING  { return { case: "=", item: string.join("") } }

    NUMBER
    = number:[0-9\.]+ { return parseFloat(number.join("")); }
    / number:[0-9]+   { return parseInt(number.join(""), 10); }

    STRING = string:[^*]+ { return string; }
`);

console.log(parser.parse("123"))
console.log(parser.parse("123.45"))
console.log(parser.parse("abcd"))
console.log(parser.parse("abcd-efgh"))
console.log(parser.parse("1abcd"))

The output as follows:

{case: "=", item: 123}
{case: "=", item: 123.45}
{case: "=", item: "abcd"}
{case: "=", item: "abcd-efgh"}

Sandbox: https://codesandbox.io/s/javascript-testing-sandbox-forked-5vekz?file=/src/index.js

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Since NUMBER matches the first character, you'll want to use a negative lookahead for STRING before returning a successful match. That way, if a STRING character is encountered, the NUMBER rule will fail and the second alternation of start will be used.

Here's an example, although as @thinkgruen has written, you'll probably want to try to parse floats less loosely as well.

start
    = number:NUMBER  { return { case: "=", item: number } } 
    / string:STRING  { return { case: "=", item: string.join("") } }

NUMBER
    = number:[0-9\.]+ (!STRING) { return parseFloat(number.join("")); }
    / number:[0-9]+   (!STRING) { return parseInt(number.join(""), 10); }

STRING = string:[^*]+ { return string; }
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda